<template>
  <!-- 歌单评论 -->
  <div class="comment">
    <!-- 评论区 -->
    <div class="area-warp" ref="areaWrapRef">
      <textarea
        class="text-area"
        v-model="commentInfo.content"
        ref="textAreaRef"
        @keyup.enter="sendComment"
      ></textarea>
      <div class="word-num">{{ restNum }}</div>
    </div>
    <!-- 按钮 -->
    <div class="btn-wrap mtop-10">
      <div>
        <button class="font-18 no-btn" @click="commentInfo.content += '@'">
          @
        </button>
        <button class="font-18 no-btn" @click="addTopic">#</button>
      </div>
      <div>
        <button class="btn btn-white" @click="sendComment">评论</button>
      </div>
    </div>
    <!-- 热门评论 -->
    <div class="mtop-20 hot-wrap" v-if="hotCommentList.length !== 0">
      <div class="font-16 font-bold">精彩评论</div>
      <div class="comment-item">
        <div
          class="comment-main"
          v-for="(item, index) in hotCommentList"
          :key="index"
        >
          <div class="img-wrap" @click="toUserDetail(item.user.userId)">
            <img class="img circle pointer" :src="item.user.avatarUrl" alt="" />
          </div>
          <div class="comment-wrap mleft-10">
            <div class="comment-content">
              <span class="font-12 pointer" style="color: #507daf">
                {{ item.user.nickname }}
              </span>
              <span class="font-12">：{{ item.content }}</span>
            </div>
            <div class="reply-content" v-if="item.beReplied.length !== 0">
              <span
                class="font-12 pointer"
                style="margin-left: 5px; color: #507daf"
                @click="toUserDetail(item.beReplied[0].user.userId)"
              >
                @{{ item.beReplied[0].user.nickname }}
              </span>
              <span class="font-12">
                {{ item.beReplied[0].content }}
              </span>
            </div>
            <div class="comment-info">
              <div class="item font-12" style="color: #9f9f9f">
                {{ item.timeStr }}
              </div>
              <!-- 点赞评论按钮 -->
              <div class="comment-btn">
                <button class="no-btn" @click="handleLike(item,'hot')">
                  <i class="el-icon-star-off" v-if="!item.liked"></i>
                  <i class="el-icon-star-on" v-else :class="{isliked :item.liked}"></i>
                  <span>{{ item.likedCount }}</span>
                </button>
                <div class="div-column"></div>
                <button class="no-btn" @click="reply(item)">
                  <i class="el-icon-chat-line-square "></i>
                </button>
                <div class="div-column"></div>
                <button class="no-btn">
                  <i class="el-icon-position"></i>
                </button>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- 最新评论 -->
    <div
      ref="newListRef"
      class="hot-wrap mtop-20"
      v-if="newCommentList.length !== 0"
    >
      <div class="font-16 font-bold">最新评论</div>
      <div class="comment-item">
        <div
          class="comment-main"
          v-for="(item, index) in newCommentList"
          :key="index"
        >
          <div class="img-wrap" @click="toUserDetail(item.user.userId)">
            <img class="img circle pointer" :src="item.user.avatarUrl" alt="" />
          </div>
          <div class="comment-wrap mleft-10">
            <div class="comment-content">
              <span
                class="font-12 pointer"
                style="color: #507daf"
                @click="toUserDetail(item.user.userId)"
              >
                {{ item.user.nickname }}
              </span>
              <span class="font-12">：{{ item.content }}</span>
            </div>
            <div class="comment-info">
              <div class="item font-12" style="color: #9f9f9f">
                {{ item.timeStr }}
              </div>
              <!-- 点赞评论按钮 -->
              <div class="comment-btn">
                <button class="no-btn" @click="handleLike(item,'new')">
                  <i class="el-icon-star-off" v-if="!item.liked"></i>
                  <i class="el-icon-star-on" v-else :class="{isliked :item.liked}"></i>
                  <span>{{ item.likedCount }}</span>
                </button>
                <div class="div-column"></div>
                <button class="no-btn" @click="reply(item)">
                  <i class="el-icon-chat-line-square "></i>
                </button>
                <div class="div-column"></div>
                <button class="no-btn">
                  <i class="el-icon-position"></i>
                </button>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { likeComment, sendComment } from "@/api/request";
import { mapState } from "vuex";
export { sendComment, likeComment } from "@/api/request";
export { mapState } from "vuex";
export default {
  props: ["hotCommentList", "newCommentList", "id", "type"],
  data() {
    return {
      commentInfo: {
        /* 1评论 2回复 0删除 */
        t: 1,
        type: this.type,
        id: this.id,
        content: "",
        commentId: 0,
      },
      replyName: "",
      identy:String, //热评 hot 最近 new
      scrollOffset : 10,
      scrollDom : 'body'
    };
  },
  computed: {
    restNum() {
      return 140 - this.commentInfo.content.length;
    },
    ...mapState(["isLogin"]),
  },
  watch:{
    'commentInfo.content'(val){
      if(val == ''){
        this.commentInfo.t = 1;
      }
    }
  },
  methods: {
    addTopic() {
      this.commentInfo.content += "#输入想说的话题#";
    },
    toUserDetail(id) {
      this.$router.push("/userdetail/" + id);
    },
    // 发送评论
    async sendComment() {
      if (!this.isLogin) return this.$message.error('需要登录')
      if (this.restNum < 0) return this.$message.error('字数过长')
      if (this.commentInfo.commentId !== 0) {
        this.commentInfo.content = this.commentInfo.content.replace(
          '回复' + this.replyName + ':',
          ''
        )
      }
      const { data: res } = await sendComment(this.commentInfo);
      console.log(res);
      if (res.code !== 200) return;
      this.$message.success("发送成功");
      this.commentInfo.content = "";
      this.commentInfo.t = 1;
      this.commentInfo.commentId = 0;
      window.setTimeout(() => {
        this.$emit("getNew");
      }, 500);
    },
    //点赞的回调
    handleLike(info , identy){
      if(!this.isLogin) return this.$message.error("需要登录");
      this.identy = identy;
      let obj = {id : this.commentInfo.id , cid : info.commentId , t : 1 , type : this.type};
      if(info.liked){
        obj.t = 0
      }
      this.like(obj , this.identy);
    },
    //点赞处理
    async like(obj , type){
      const {data:res} = await likeComment(obj);
      if(res.code !== 200) return ;
      if(type === 'new'){
        let index = this.newCommentList.findIndex((item) => item.commentId == obj.cid);
        this.newCommentList[index].liked = !this.newCommentList[index].liked;
        if(obj.t){
          this.newCommentList[index].likedCount++;
        } else {
          this.newCommentList[index].likedCount--;
        }
      } else if (type === 'hot'){
        let index = this.hotCommentList.findIndex((item) => item.commentId == obj.cid);
        this.hotCommentList[index].liked = !this.hotCommentList[index].liked;
        if(obj.t){
          this.hotCommentList[index].likedCount++;
        } else {
          this.hotCommentList[index].likedCount--;
        }
      }
    },
    //回复处理
    reply(info){
      console.log(this.newCommentList);
      if(!this.isLogin) return this.$message.error("需要登录");
      this.replyName = info.user.nickname;
      this.commentInfo.content = '回复' + info.user.nickname + ':';
      this.commentInfo.commentId = info.commentId;
      this.commentInfo.t = 2;
      this.toTopAnimation(
        this.$refs.areaWrapRef.offsetTop - this.scrollOffset,
        document.querySelector(this.scrollDom),
        600
      )
      this.$nextTick(() => {
        this.$refs.textAreaRef.focus();
      })
    },
     /* 滚动条由下至上的动画js动画 */
     toTopAnimation(target, scrollDom, ms = 500) {
      let start
      let begin = scrollDom.scrollTop
      let size = (begin - target) / ms
      const step = (timestamp) => {
        if (start === undefined) start = timestamp
        const elapsed = timestamp - start
        /* 防止上滑过头 */
        scrollDom.scrollTop = Math.max(begin - size * elapsed, target)
        if (elapsed < ms) {
          // 在ms毫秒后停止动画
          window.requestAnimationFrame(step)
        }
      }
      window.requestAnimationFrame(step)
    }
  },
};
</script>

<style lang="less" scoped>
.area-warp {
  position: relative;
  .text-area {
    box-sizing: border-box;
    width: 100%;
    height: 60px;
    border: 1px solid #e5e5e5;
    outline: none;
    font-size: 14px;
    border-radius: 4px;
    padding: 4px 1px;
    resize: none;
    word-break: break-all;
    word-wrap: break-word;
  }
  .word-num {
    position: absolute;
    bottom: 4px;
    right: 4px;
    color: #dfcfdf;
    font-size: 16px;
  }
}
.btn-wrap {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.comment-main {
  display: flex;
  align-items: center;
  margin: 10px 0;
  .img-wrap {
    width: 40px;
    height: 40px;
  }
  .comment-wrap {
    line-height: 30px;
    flex: 1;
    .comment-info {
      display: flex;
      justify-content: space-between;
    }
    .comment-content {
      word-break: break-all;
      word-wrap: break-word;
    }
    .reply-content {
      background-color: #f4f4f4;
      border-radius: 6px;
      word-break: break-all;
      word-wrap: break-word;
    }
  }
}
.div-column {
  display: inline-block;
  width: 1px;
  height: 14px;
  background-color: #dfcfe7;
}
.isliked {
  color: red;
}
</style>